
package uk.ac.cam.cl.oscar.Oscar_Mobile_Server;

import java.io.*;
import java.util.*;
import uk.ac.cam.cl.oscar.*;
import uk.ac.cam.cl.oscar.sift.*;

public class SendResponse {
    public void send(ImageRecord[] matches, DataOutputStream dos){
        
        try{
            dos.writeInt(matches.length);
            //dos.writeUTF(mathce);
            
            if(Demonstration.DemoAppInst != null){
                Demonstration.DemoAppInst.displayNewMatch(matches[0].getThumbnail());
            }
            
            dos.writeUTF(matches[0].getPosterBlurb());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            javax.imageio.ImageIO.write(matches[0].getThumbnail(), "JPEG", baos);
            byte[] temp = baos.toByteArray();
            int length = temp.length;
            dos.writeInt(length);
            dos.write(temp, 0, temp.length);
            
        }catch(Exception e){
            
        }
        
        
        
        /*try{
            int length = matches.length;
            int[] pictureSizes = {0,0,0};
            byte[] buffer;
            
            //order matches array in terms of score
            ImageRecord recordBuffer;
            for(int j = 0; j < (length - 1); j++){
                if(length == 1) continue;
                for(int i = 0; i < length -1; i++){
                    recordBuffer = matches[i];
                    if(matches[i].getScore() < matches[i+1].getScore()){
                        matches[i] = matches[i+1];
                        matches[i+1] = recordBuffer;
                    }
                }
            }
            
            //Concatenate thumbnails into single byte array
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
            javax.imageio.ImageIO.write(matches[0].getThumbnail(), "JPEG", baos1);
            buffer = baos1.toByteArray();
            pictureSizes[0] = buffer.length;
            
            for(int i = 1; i < length; i++){
                if(matches.length == 1) break;
                ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
                javax.imageio.ImageIO.write(matches[i].getThumbnail(), "JPEG", baos2);
                byte[] imageBuffer = baos2.toByteArray();
                int imageSize = imageBuffer.length;
                pictureSizes[i] = imageSize;
                
                byte[] bufferCopy = new byte[buffer.length];
                System.arraycopy(buffer, 0, bufferCopy, 0, buffer.length);
                buffer = new byte[bufferCopy.length+imageBuffer.length];
                System.arraycopy(bufferCopy, 0, buffer, 0, bufferCopy.length);
                System.arraycopy(imageBuffer, 0, buffer, bufferCopy.length, imageBuffer.length);
            }
            
            //Send number of image records returned
            dos.writeInt(length);
            
            //Send the size in bytes of each thumbnail
            for(int i = 0; i < 3; i++){
                dos.writeInt(pictureSizes[i]);
            }
            
           
            
            //Send picture info
            for(int i = 0; i < length; i++){
                dos.writeUTF(matches[i].getArtist());
                dos.writeUTF(matches[i].getPosterId());
                dos.writeUTF(matches[i].getPosterBlurb());
                dos.writeUTF(matches[i].getCost());
                //calulcate score to send and send it, send as string as older mobile java implementation can't deal with floats
                double scoreFloat = matches[i].getScore();
                int scoreInt = (int)(scoreFloat*100f);
                String score = String.format("%d", scoreInt);
                dos.writeUTF(score);
            }
            
            //Send buffer containing the thumbnails
            dos.write(buffer, 0, buffer.length);
            
        }
        catch(IOException e){
            
        }*/
    }

}
